package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import CtrlLayer.CtrlLayer;
import CtrlLayer.PersonCtrl;
import GUIadapter.PanelTableModelAdapter;
import GUIadapter.SupplierModel;
import ModelLayer.Supplier;

@SuppressWarnings("serial")
public class PanelLeverandoer extends JPanel implements Updateable
{
	private List<Supplier> supplierList;
	private PanelTableModelAdapter panelTableSupplier;
	private int indexTableSupplier;
	private PersonCtrl personCtrl;
	private PopupLeverandrdetaljer popupLeverandrdetaljer;
	private PopupOpretLeverandr popupOpretLeverandr;
	private PopupRedigerLeverandr popupRedigerLeverandr;
	private PopupBestilVare popupBestilVare;

	public PanelLeverandoer()
	{
		// Dear PopupWindow, please remember "this",
		// so you know which "Updateable" you came from..!
		popupOpretLeverandr = new PopupOpretLeverandr(this);
		popupLeverandrdetaljer = new PopupLeverandrdetaljer(this);
		popupRedigerLeverandr = new PopupRedigerLeverandr(this);
		popupBestilVare = new PopupBestilVare(this);
		
		
	

		JButton BestilVare = new JButton("Bestil vare");
		
		BestilVare.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupBestilVare.setVisible(true);
			}
		});

		JButton Leverandrdetaljer = new JButton("Leverand\u00F8rdetaljer");
		
		Leverandrdetaljer.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupLeverandrdetaljer.resetLeverandr();
				popupLeverandrdetaljer.setVisible(true);
			}
		});
		
		JButton RedigerLeverandr = new JButton("Rediger Leverand\u00F8r");
		RedigerLeverandr.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupRedigerLeverandr.resetLeverandr();
				popupRedigerLeverandr.setVisible(true);
			}
		});
		
				
		RedigerLeverandr.addActionListener(new ActionListener()
		
		{
			public void actionPerformed(ActionEvent arg0)
			{
				if ((indexTableSupplier >= 0) && (indexTableSupplier < supplierList.size()))
				{
					update();
				}
			}
		});

		JButton NyLeverandr = new JButton("Ny leverand\u00F8r");
		NyLeverandr.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupOpretLeverandr.clearFields();
				popupOpretLeverandr.setVisible(true);
			}
		});

		CtrlLayer cl = new CtrlLayer();

		JPanel panelDate = new JPanel();
		panelDate.setAlignmentX(Component.RIGHT_ALIGNMENT);
		panelDate.setBorder(new TitledBorder(null,"Dato",TitledBorder.LEADING,
				TitledBorder.TOP,null,null));
		
		

		personCtrl = new PersonCtrl();

		supplierList = personCtrl.getSupplierList();

		panelTableSupplier = new PanelTableModelAdapter(new SupplierModel(supplierList));

		panelTableSupplier.setBorder(new TitledBorder(new LineBorder(new Color(192,192,192)),
									"Leverand\u00F8r(er)",TitledBorder.LEFT,TitledBorder.TOP,null,new Color(51,51,51)));
		panelTableSupplier.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		panelTableSupplier.getTable().getSelectionModel()
				.addListSelectionListener(new ListSelectionListener()
				{
					@Override
					public void valueChanged(ListSelectionEvent event)
					{
						if (event.getValueIsAdjusting() == false)
						{
							indexTableSupplier = panelTableSupplier.getTable()
								.getSelectedRow();
						}
					}
				});

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panelTableSupplier, GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(RedigerLeverandr, 0, 0, Short.MAX_VALUE)
								.addComponent(NyLeverandr, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(Leverandrdetaljer, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(18)
							.addComponent(BestilVare)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(panelDate, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
							.addGap(57))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(NyLeverandr)
								.addComponent(BestilVare))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(Leverandrdetaljer)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(RedigerLeverandr))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(24)
							.addComponent(panelDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addComponent(panelTableSupplier, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(88, Short.MAX_VALUE))
		);

		JLabel lblNewLabel = new JLabel("Dato: " + cl.getDateString());
		panelDate.add(lblNewLabel);
		lblNewLabel.setFont(new Font("Tahoma",Font.PLAIN,15));
		setLayout(groupLayout);	
		// TODO Auto-generated constructor stub
	}

	@Override
	public void update()	{
		panelTableSupplier.getTable().revalidate();
		panelTableSupplier.getTable().repaint();
	}
}
